﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KhachSan.Process.Private.Tien.Forms;

namespace KhachSan.Process.Private.Tien.Usercontrol
{
    public partial class ucFormCheckIn_Input : UserControl
    {
        KhachSanDataContext db = new KhachSanDataContext(Public.XLDuLieu.chuoi_ket_noi);
        private int songuoi = 0;
        
        public ucFormCheckIn_Input()
        {
            InitializeComponent();
            panel2.Left = Screen.PrimaryScreen.Bounds.Width/2 - panel2.Size.Width/2;
            panel3.Left = Screen.PrimaryScreen.Bounds.Width / 2 - panel3.Size.Width / 2;
            panel6.Left = Screen.PrimaryScreen.Bounds.Width / 2 - panel6.Size.Width / 2;

            Public.XLDuLieu.LoadCombobox("select * from GIAPHONG", "DIENGIAI", "MAHANGPHONG", cbHP);

            cbLoaiKhach.SelectedIndex = 0;
            updateDonGia_MaPhong();
            updateSoNguoi();
        }

        private void EmptyTextBox()
        {tbTenDoan.Text = "";
            tbLienHe.Text = "";
            tbDienThoai.Text = "";
            tbDTDD.Text = "";
            tbNgayDen.Text = "";
            tbGioDen.Text = "";
            tbNgayDi.Text = "";
            tbGioDi.Text = "";
            tbThemGiuong.Text = "";
        }

        private void cbHP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbHP.SelectedValue != null)
            {
                updateDonGia_MaPhong();
                updateSoNguoi();
            }
        }

        void updateSoNguoi()
        {
            DataTable table = Public.XLDuLieu.LoadDataTable("select MAKP from LOAI_KIEUPHONG where MAHANGPHONG = '" + cbHP.SelectedValue.ToString() + "'");

            if (table.Rows.Count > 0)
            {
                string nguoi = table.Rows[0][0].ToString().Trim();
                if (nguoi == "DOUB")
                {
                    lblSoNguoi.Text = "2 người (1 giường đôi)";
                    songuoi = 2;
                }
                else if (nguoi == "TRIP")
                {
                    lblSoNguoi.Text = "3 người (3 giường chiếc/1 đôi 1 chiếc)";
                    songuoi = 3;
                }
                else if (nguoi == "TWIN")
                {
                    lblSoNguoi.Text = "2 người (Phòng có 2 giường chiếc)";
                    songuoi = 2;
                }
            }
        }
        void updateDonGia_MaPhong()
        {
            //đổi đơn giá
            dgvDonGia.DataSource = null;
            dgvDonGia.DataSource = db.sp_DonGia(cbHP.SelectedValue.ToString());

            //đổi danh sách mã phòng
            cbMaPhong.Items.Clear();
            DataGridView dgv = new DataGridView();
            dgv.Visible = false;
            this.Controls.Add(dgv);
            dgv.DataSource = db.sp_MaPhong(cbHP.SelectedValue.ToString());
            if (dgv.RowCount > 1)
            {
                for (int i = 0; i < dgv.RowCount - 1; i++)
                {
                    cbMaPhong.Items.Add(dgv.Rows[i].Cells[0].Value.ToString());
                }
                cbMaPhong.SelectedIndex = 0;
            }
        }

        private void cbLoaiKhach_SelectedIndexChanged(object sender, EventArgs e)
        {
            //thể hiện mức giảm giá
            try
            {
                if (cbLoaiKhach.SelectedIndex == 0)
                {
                    Public.XLDuLieu.LoadCombobox("select * from CONGTY", "TENCTY", "ID_CTY", cbCTy);
                    string s = cbCTy.SelectedValue.ToString();
                    DataGridView dgv = new DataGridView();
                    dgv.Visible = false;
                    this.Controls.Add(dgv);
                    dgv.DataSource = db.sp_MucGiamGiaCty(Int32.Parse(s));
                    tbGiamGia.Text = dgv.Rows[0].Cells[0].Value.ToString() + "%";
                }
                else if (cbLoaiKhach.SelectedIndex == 1)
                {
                    Public.XLDuLieu.LoadCombobox("select * from CONGTY_LUHANH", "TENCTY", "ID_CTYLH", cbCTy);
                    string s = cbCTy.SelectedValue.ToString();
                    DataGridView dgv = new DataGridView();
                    dgv.Visible = false;
                    this.Controls.Add(dgv);
                    dgv.DataSource = db.sp_MucGiamGiaCtyLH(Int32.Parse(s));
                    tbGiamGia.Text = dgv.Rows[0].Cells[0].Value.ToString() + "%";
                }
            }
            catch{}
        }

        private void cbCTy_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //show mức giảm giá
                if (cbLoaiKhach.SelectedIndex == 0)
                {
                    string s = cbCTy.SelectedValue.ToString();
                    DataGridView dgv = new DataGridView();
                    dgv.Visible = false;
                    this.Controls.Add(dgv);
                    dgv.DataSource = db.sp_MucGiamGiaCty(Int32.Parse(s));
                    tbGiamGia.Text = dgv.Rows[0].Cells[0].Value.ToString() + "%";
                }
                else if (cbLoaiKhach.SelectedIndex == 1)
                {
                    string s = cbCTy.SelectedValue.ToString();
                    DataGridView dgv = new DataGridView();
                    dgv.Visible = false;
                    this.Controls.Add(dgv);
                    dgv.DataSource = db.sp_MucGiamGiaCtyLH(Int32.Parse(s));
                    tbGiamGia.Text = dgv.Rows[0].Cells[0].Value.ToString() + "%";
                }
            }catch{}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(cbMaPhong.Text,tbNgayDen.Text,tbGioDen.Text,tbNgayDi.Text,tbGioDi.Text,cbMaPhong.Text,tbThemGiuong.Text,tbTraThem.Text,tbGiamGia.Text,tbDaThanhToan.Text,"");
            EmptyTextBox();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            Public.TestTextBox.OnlyNumber(e);
            if (char.IsNumber(e.KeyChar))
            {
                tbThemGiuong.Text =
                    ((int.Parse(e.KeyChar.ToString()) - songuoi)*int.Parse(dgvDonGia.Rows[0].Cells[4].Value.ToString()))
                        .
                        ToString();
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            Public.TestTextBox.OnlyNumber(e);
            if (char.IsNumber(e.KeyChar))
            {
                textBox3.Text =
                    ((int.Parse(e.KeyChar.ToString())) * int.Parse(dgvDonGia.Rows[0].Cells[5].Value.ToString())).ToString();
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex == 11)
            {
                Forms.F_KhachThue f = new F_KhachThue();
                f.ShowDialog();
            }
        }

    }
}
